//
//  NewsCollectionListCell.m
//  ShowYork
//
//  Created by hxh on 2021/1/18.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "NewsCollectionListCell.h"

@interface NewsCollectionListCell ()
@property (nonatomic,strong)UIImageView *newsPic;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *dateLb;
@property (nonatomic,strong)UILabel *watchLb;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation NewsCollectionListCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    

    self.newsPic = [[UIImageView alloc]init];
    self.newsPic.layer.cornerRadius = 4;
    self.newsPic.contentMode = UIViewContentModeScaleAspectFill;
    self.newsPic.backgroundColor = C(240, 1);
    self.newsPic.layer.masksToBounds = YES;
    [self.contentView addSubview:self.newsPic];
    [self.newsPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.width.mas_equalTo(KScaleWidth(120));
        make.height.mas_equalTo(KScaleWidth(120)/4*3).priorityHigh();
        make.bottom.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
 
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"全国高考延迟一个月后，多少人心态崩了";
    self.titleLb.textColor = C51;
    self.titleLb.numberOfLines = 2;
    self.titleLb.font = font(16);
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.newsPic).offset(4);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.newsPic.mas_left);
    }];

    self.dateLb = [[UILabel alloc]init];
    self.dateLb.text = @"2020-11-02 21:00";
    self.dateLb.textColor = C153;
    self.dateLb.font = font(10);
    [self.contentView addSubview:self.dateLb];
    [self.dateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLb);
        make.top.equalTo(self.titleLb.mas_bottom).offset(16);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = C(226, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.dateLb);
        make.left.equalTo(self.dateLb.mas_right).offset(12);
        make.size.mas_equalTo(CGSizeMake(0.5, 10));
    }];
    
    self.watchLb = [[UILabel alloc]init];
    self.watchLb.text = @"3.2w浏览量";
    self.watchLb.textColor = C153;
    self.watchLb.font = font(10);
    [self.contentView addSubview:self.watchLb];
    [self.watchLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.dateLb);
        make.left.equalTo(self.line.mas_right).offset(12);
    }];
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"NewsCollectionListCell";
    NewsCollectionListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[NewsCollectionListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setItemModel:(NewsListItemModel *)itemModel{
    _itemModel = itemModel;
    [self.newsPic sd_setImageWithURL:[NSURL URLWithString:itemModel.cover]];
    self.titleLb.text = itemModel.title;

    NSString *timeStr = [RPAppManager created_atWithCreateDate:itemModel.projectCreateTime.longValue andCurrentDate:0];
    
    self.dateLb.text = timeStr;
    
    if (itemModel.pageView.intValue == 0) {
        self.line.hidden = YES;
        self.watchLb.hidden = YES;
    }else{
        self.line.hidden = NO;
        self.watchLb.hidden = NO;
        
        if (itemModel.pageView.intValue >= 10000) {
            NSInteger count = itemModel.pageView.intValue;
            if (count % 1000 == 0) {
                count += 1;
            }
            self.watchLb.text = [NSString stringWithFormat:@"%@w浏览量",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
        }else{
            self.watchLb.text = [NSString stringWithFormat:@"%@浏览量",itemModel.pageView];
        }
    }
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showNewsDetailByCurrentModel:)]) {
        [self.delegate showNewsDetailByCurrentModel:self.itemModel];
    }
}


@end
